using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using TidhiGanitam.DataModel;

namespace TidhiGanitam.Ganitam
{
    public class Kujudu
    {
        Rasi m_MadyamaKujudu;
        Rasi m_KujaMandochha;
        //Rasi m_RaviKendram;
        Rasi m_KujaKshepaPatamu;
        DateTime m_Ugadi;
        ArrayList m_DailyRoutines;
        
        public Kujudu()
        {
            m_MadyamaKujudu = new Rasi();
            m_KujaMandochha = new Rasi();
            //m_RaviKendram = new Rasi();
            m_KujaKshepaPatamu = new Rasi();
            m_DailyRoutines = new ArrayList();
           
        }
        public Kujudu(Rasi MadyamaKujudu, Rasi KujaMandochha, Rasi KujaKshepaPatamu)
        {
            
            m_MadyamaKujudu = MadyamaKujudu;
            m_KujaMandochha = KujaMandochha;
            m_KujaKshepaPatamu = KujaKshepaPatamu;
            m_DailyRoutines = new ArrayList();
             
        }
        
        public Rasi MadyamaKujudu
        {
            get { return m_MadyamaKujudu; }
        }
        public Rasi KujaMandochha
        {
            get { return m_KujaMandochha; }
        }
        public Rasi KujaKshepaPatamu
        {
            get { return m_KujaKshepaPatamu; }
        }
        /*
        public Rasi RaviKendram
        {
            get { return m_RaviKendram; }
        }*/
        public ArrayList DailyCalculations
        {
            get { return m_DailyRoutines; }
        }
        
        public void DoKujaSamskaram(int Year, Abdapudu abdapudu, YearDetails CurrentYearDetails)
        {
            
            /********************************************************
             * 1. Get MadyamaKujudu, KujaMandochha, KujaKshepaPatamu for the Salivahana year
             * 2. These are Sayana Grahalu - With Iyanamsa
             * 3. Chalana Phala Samskaram
             * 4. Tidhula Samskaram
             * 5. Iyamamsa Samskaram
             * 6. Subtract RaviMandochha from MadyamaRavi = RaviKendram
             * *******************************************************/
            
            //First Note the Ugadi datetime
            m_Ugadi = abdapudu.UgadiDate;
            //Step 1 - Madyama Kujudu, KujaMandochha, KujaKshepaPatamu for salivahana year
            KujuduItem kujuduItem = SaliVahanaSakam.GetKujuduValues(Year);
            
            m_MadyamaKujudu = kujuduItem.MadyamaKujudu;
            m_KujaMandochha = kujuduItem.KujaMandochha;
            m_KujaKshepaPatamu = kujuduItem.KujaKshepaPatamu;
            /*
            //Step 3 - Chalana phala Samskaram
            //This is done only for MadyamaRavi
            Rasi TempChalanaPhalam = new Rasi(0,abdapudu.ChalanaPhalam.RawValue);
            Rasi chalanaPhalaValue = TempChalanaPhalam * Constants.RaviDinaGati;
            //ikkada / new Rasi(0, 0, 60, 0) vadiliveste correct value vacchindi
            //Round the chalanaphalam value
            chalanaPhalaValue = new Rasi(chalanaPhalaValue.Rasulu, chalanaPhalaValue.Bhagalu, chalanaPhalaValue.Liptalu, chalanaPhalaValue.Viliptalu);
            if (abdapudu.ChalanaPhalamSign == Enums.ChalanaPhalam.Runam)
            {
                m_MadyamaRavi -= chalanaPhalaValue;
            }
            else 
            {
                m_MadyamaRavi += chalanaPhalaValue;
            }

            //Step 4 - Tidhula Samskaram
            //This is done only for MadyamaRavi
            Rasi tidhulaGatiValue = Constants.RaviDinaGati * abdapudu.Tidhulu;
            m_MadyamaRavi -= tidhulaGatiValue;

            //Step 5 - Iyanamsa Samskaram
            //This is done for both MadyamaRavi and RaviMandochha

            m_MadyamaRavi -= CurrentYearDetails.UgadiIyanamsamu;
            m_RaviMandochha -= CurrentYearDetails.UgadiIyanamsamu;

            //Round the Madyama Ravi and Mandochha
            m_MadyamaRavi = m_MadyamaRavi.Round();
            m_RaviMandochha = m_RaviMandochha.Round();
            
            //Step 6 - Ravi Kendram
            // Subtract Ravi Mandochha from Madhyama Ravi.
            m_RaviKendram = m_MadyamaRavi - m_RaviMandochha;
            */
        }
        public void DoKujuduRoutinesForYear()
        {
            //16/11 - Raghu - Entire calculations are not yet finished. this is skeleton only.
            int iSNo = 0;
            ArrayList ArrKujudu = new ArrayList();
            //The original KujaKendram is needed for calculations of chandra,
            //We take the copies of Ravi Kendram and Madyama Ravi and then proceed
            Rasi KujaKendram = new Rasi(m_MadyamaKujudu);
            Rasi MandaSpastaKujudu = new Rasi(m_KujaMandochha);
            for (; iSNo < 400; iSNo++)
            {
                KujuduRoutineItem KujuduRoutine = new KujuduRoutineItem(iSNo, KujaKendram, MandaSpastaKujudu, m_Ugadi.AddDays(iSNo));
                KujuduRoutine.Calculate();
                ArrKujudu.Add(KujuduRoutine);
                KujaKendram += Constants.RaviDinaGati; //here i need to change RaviGati to kujaGati
                MandaSpastaKujudu += Constants.RaviDinaGati;
                //Round values 
                KujaKendram.Refresh();
                MadyamaKujudu.Refresh();
            }
            m_DailyRoutines = ArrKujudu;
        }
    }
}
